<template>
  <div>
    <el-container style="height: 840px; border: 1px solid #eee">
      <el-aside width="200px" style="background-color: rgb(238, 241, 246)">
        <el-menu :default-openeds="['1']" :router=true>
          <el-menu-item index="/allNews"><i class="el-icon-s-home">首页</i></el-menu-item>
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-menu">新闻分类</i>
            </template>
            <el-menu :router=true :default-active="activeIndex">
              <el-menu-item index="/entertainment"><i class="el-icon-orange">娱乐</i></el-menu-item>
              <el-menu-item index="/political"><i class="el-icon-reading">时政</i></el-menu-item>
              <el-menu-item index="/international"><i class="el-icon-menu">国际</i></el-menu-item>
              <el-menu-item index="/sports"><i class="el-icon-basketball">体育</i></el-menu-item>
              <el-menu-item index="/life"><i class="el-icon-coffee-cup">生活</i></el-menu-item>
              <el-menu-item index="/economic"><i class="el-icon-data-line">财经</i></el-menu-item>
            </el-menu>
          </el-submenu>
        </el-menu>
      </el-aside>

      <el-container>
        <el-header style="text-align: right; font-size: 12px">
        
          <el-dropdown>
            <i class="el-icon-setting" style="margin-right: 15px"></i>
            <el-dropdown-menu slot="dropdown">
             <el-dropdown-item>
                <el-button type="text" v-on:click="apply()" v-if="user.userKind=='普通用户'">申请成为创作者</el-button>
                <el-button type="text" v-on:click="apply()" v-if="user.userKind=='申请成为创作者'">申请成为创作者</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button type="text" v-on:click="addition()" v-if="user.userKind=='管理员'">发布新闻</el-button>
                <el-button type="text" v-on:click="addition()" v-if="user.userKind=='创作者'">发布新闻</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button type="text" v-on:click="person()">个人空间</el-button>
              </el-dropdown-item>
              <el-dropdown-item>
                <el-button type="text" v-on:click="logout()">注销</el-button>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
         <el-button type="primary" icon="el-icon-receiving" plain v-if="user.userKind=='管理员'" v-on:click="admin()">
            管理员页面
          </el-button>
          <el-button type="primary" icon="el-icon-s-custom" plain v-on:click="login()">
            <span v-if="user.userName">{{user.userName}}</span>
            <span v-else>登录/注册</span>
          </el-button>
        </el-header>

        <el-main>
          <router-view/>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script>
export default {
  name: "MainTable",
  data() {
    return {
      activeIndex: 'entertainment',
      user: {
                userId:"",
                userName: "",
                userKind:""
            },//用来存放用户登录信息
    }
  },
    methods: {
    logout(){
         var userString = localStorage.getItem("user");
            if (userString) {
                localStorage.removeItem("user");
                this.user={};
          alert("注销成功")
            }else{
              alert("当前没有用户");
            }
        
        },
        login(){
          if(this.user.userName){
            alert("已登录");
          }else{
            location.href = "#/login";
          }
        },
        addition(){
          location.href = "#/addition";
        },
        apply(){
           if(this.user.userKind=="申请成为创作者"){
              alert("您已发送申请，无需重复申请");
          }else{
              this.$axios.post("http://localhost:9090/mynewsweb/api/applytoWriter", this.user).then((res) => {
            console.log(res.data);
            if (res.data.state) {
              alert(res.data.msg);     
                localStorage.removeItem("user");
                localStorage.setItem("user",JSON.stringify(res.data.user));  
                var userString = localStorage.getItem("user");
                if (userString) {
                  this.user = JSON.parse(userString);
                } 
            } else {
              alert(res.data.msg);
            }
          });
}
          
        },
        admin(){
          location.href = "#/administrator";
        },
        person(){
           var userString = localStorage.getItem("user");
            if (userString) {
               location.href = "#/personalSpace";
            }else{
              alert("请先登录");
            }
           
        }
  },
    created(){
      var userString = localStorage.getItem("user");
      if (userString) {
                this.user = JSON.parse(userString);
      }
      //获取财经新闻
      this.$router.push("/allNews");
  }
};
</script>

<style>
.el-header {
  background-color: #b3c0d1;
  color: #333;
  line-height: 60px;
}

.el-aside {
  color: #333;
}
</style>